﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 小小次朗点餐系统
{
    public partial class Form6 : Form
    {
        public static int gj = 0;//退出系统还是关闭窗口

        public static DataTable dt = new DataTable();

        //记录前一画面修改的行号
        public static int index = 0;
        public Form6()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            dataGridView1.Visible = false;
            dataGridView2.Visible = true;
            contextMenuStrip1.Enabled = false;
            contextMenuStrip2.Enabled = true;
            panel3.Visible = false;
            //获取当前登录账户的id
            string sqlseID = string.Format($"select SeID from Seller where Senumber='{userLogin.suserName}' and Pwd='{userLogin.spassWord}'");
            string SeID = DBHelper.ExecuteScalar(sqlseID).ToString();
            string sql = string.Format($"select * from Food");
            DataSet ds = DBHelper.SelectByAdapter(sql);
            dt = ds.Tables[0];
            //不显示多余列
            dataGridView2.AutoGenerateColumns = false;
            dataGridView2.DataSource = dt;
        }

        private void button17_Click(object sender, EventArgs e)
        {
            dataGridView1.Visible = true;
            dataGridView2.Visible = false;
            contextMenuStrip1.Enabled = true;
            contextMenuStrip2.Enabled = false;
            panel3.Visible = false;

            //获取当前登录账户的id
            string sqlseID = string.Format($"select SeID from Seller where Senumber='{userLogin.suserName}' and Pwd='{userLogin.spassWord}'");
            string SeID = DBHelper.ExecuteScalar(sqlseID).ToString();

            string sql = string.Format($"select * from Merchants m,Seller s,[Order] o,Detailed d where m.SeID= s.SeID and o.OrderID=d.OrderID and d.MeID=m.MeID and s.SeID='{SeID}'");
            DataSet ds = DBHelper.SelectByAdapter(sql);

            dt = ds.Tables[0];
            dataGridView1.AutoGenerateColumns = false;
            dataGridView1.DataSource = dt;

        }

        private void button2_Click(object sender, EventArgs e)
        {
            dataGridView1.Visible = false;
            dataGridView2.Visible = false;
            contextMenuStrip1.Enabled = false;
            contextMenuStrip2.Enabled = false;
            panel3.Visible = true;

            string sql = string.Format($"select * from Seller where Senumber='{Accounts.accounts}'");
            DataSet ds = DBHelper.SelectByAdapter(sql);
            dt = ds.Tables[0];
            if (dt.Rows.Count > 0)
            {
                //加载字段
                xxx1.Text = dt.Rows[0][1].ToString();
                xxx2.Text = dt.Rows[0][2].ToString();
                xxx3.Text = dt.Rows[0][4].ToString();
            }
            }
        private void button3_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        private void Form6_FormClosing(object sender, FormClosingEventArgs e)//判断是否退出系统
        {
            if (gj == 0)
            {
                DialogResult rlt = MessageBox.Show("确认关闭本窗口吗？", "提示：", MessageBoxButtons.YesNo);
                if (rlt == DialogResult.Yes)
                {
                    userLogin userLogin = new userLogin();
                    userLogin.Show();
                }
                else
                {
                    e.Cancel = true;
                }
            }
            if (gj == 1)
            {
                DialogResult rlt = MessageBox.Show("确认退出系统吗？", "提示：", MessageBoxButtons.YesNo);
                if (rlt == DialogResult.Yes)
                {
                    System.Environment.Exit(0);
                }
                else
                {
                    e.Cancel = true;
                }
            }
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void Form6_Load(object sender, EventArgs e)
        {

        }

        private void contextMenuStrip1_Opening(object sender, CancelEventArgs e)
        {

        }

        private void dataGridView2_Load(object sender, DataGridViewCellEventArgs e)
        {


        }

        private void contextMenuStrip2_Opening(object sender, CancelEventArgs e)
        {

        }

        private void 修改ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //添加修改的识别变量设定1
            Accounts.type = "1";
            //取得用户选择的修改行号  传递给共享变量
            Form5.index = this.dataGridView2.CurrentRow.Index;
            //调用添加画面
            Form5 frm5 = new Form5();
            frm5.Show();

        }

        private void 添加ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //添加修改的识别变量设定0
            Accounts.type = "0";
            //调用添加画面
            Form5 frm5 = new Form5();
            frm5.Show();

        }

        private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DialogResult rlt = MessageBox.Show("确定要删除吗？", "删除确认", MessageBoxButtons.YesNo);
            if (rlt == DialogResult.Yes)
            {
                //数据库删除
                int number = dataGridView2.CurrentRow.Index;
                string sql = string.Format($"delete from Food where FoodID ={dataGridView2.Rows[number].Cells["Column10"].Value}");
                bool result = DBHelper.GetNonQuery(sql);
                if (result)
                {
                    MessageBox.Show("删除成功");
                    dt.Rows.RemoveAt(number);
                }
            }
        }

        private void 接单ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string state = "接单中";
            ///取得用户选择的行号
            int index = this.dataGridView1.CurrentRow.Index;
            string oid = dt.Rows[index]["OrderID"].ToString();

            string sql1 = string.Format($"update [Order] set OrderState='{state}' where OrderID='{oid}'");
            bool result = DBHelper.GetNonQuery(sql1);
            if (result)
            {
                MessageBox.Show("修改成功");
            }
        }

        private void 退单ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //获取当前登录账户的id
            string sqlseID = string.Format($"select SeID from Seller where Senumber='{userLogin.suserName}' and Pwd='{userLogin.spassWord}'");
            string SeID = DBHelper.ExecuteScalar(sqlseID).ToString();
            string sql = string.Format($"select OrderState from Merchants m,Seller s,[Order] o,Detailed d where m.SeID= s.SeID and o.OrderID=d.OrderID and d.MeID=m.MeID and s.SeID='{SeID}'");
            string ds = DBHelper.ExecuteScalar(sql).ToString();

            ///取得用户选择的行号
            int index = this.dataGridView1.CurrentRow.Index;
            string oid = dt.Rows[index]["OrderID"].ToString();

            if (ds == "接单中" || ds == "派送中" || ds=="等待接单")
            {
                DialogResult rlt = MessageBox.Show("确定取消订单吗？", "提示：", MessageBoxButtons.YesNo);
                if (rlt == DialogResult.Yes)
                {
                    string sql1 = ($"delete from Detailed  where OrderID='{oid}'");
                    bool result1 = DBHelper.GetNonQuery(sql1);
                    string sql2 = ($"delete from [Order] where OrderID='{oid}'");
                    bool result = DBHelper.GetNonQuery(sql2);
                    if (result && result1)
                    {
                        
                        //退款
                        string sqlid = string.Format($"select UserID from Merchants m,Seller s,[Order] o,Detailed d where m.SeID= s.SeID and o.OrderID=d.OrderID and d.MeID=m.MeID and s.SeID='{SeID}'");
                        string id = DBHelper.ExecuteScalar(sqlid).ToString();//查询ID

                        string sqlmoney = string.Format($"select ShoppingPrice from Merchants m,Seller s,[Order] o,Detailed d where m.SeID= s.SeID and o.OrderID=d.OrderID and d.MeID=m.MeID and s.SeID='{id}'");
                        decimal money =decimal.Parse( DBHelper.ExecuteScalar(sqlmoney).ToString());//退款钱

                        string sqlxmoney = string.Format($"select Money from [User] where UserID='{id}'");
                        decimal xmoney = decimal.Parse(DBHelper.ExecuteScalar(sqlxmoney).ToString());//现钱

                        string sqlumoney = ($"update [User] set Money='{xmoney+money}' where UserID='{id}'");
                        bool resultm = DBHelper.GetNonQuery(sqlumoney);
                        if (resultm)
                        {
                            MessageBox.Show("成功取消");
                        }
                        dt.Rows.RemoveAt(index);
                    }
                }
            }
            if (ds == "已送达")
            {
                MessageBox.Show("已送达，不可取消");
            }
        }

        private void dataGridView2_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void panel3_Paint(object sender, PaintEventArgs e)
        {

        }

        private void button4_Click(object sender, EventArgs e)
        {
            //添加修改的识别变量设定1
            Accounts.type = "2";
            
            //调用添加画面
            Form5 frm5 = new Form5();
            frm5.Show();
        }
    }
}
